//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//
//                         @王帅
package com.basics.controller;

import com.aliyuncs.exceptions.ClientException;
import com.basics.SmsVerify.SmsVerify;
import com.basics.model.BackResult;
import com.basics.model.UserAdmin;
import com.basics.service.UserAdminService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
@RequestMapping("/userAdmin")

public class UserAdminController {
    private Logger log = Logger.getLogger(UserAdminController.class);
    @Resource
    private UserAdminService userAdminService;

    @RequestMapping("/login")
    @ResponseBody
    public BackResult showUser(HttpServletRequest request) {
        log.info("后台登录");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String vercode = request.getParameter("vercode");

        HttpSession session = request.getSession();
        String vercodeServer = (String) session.getAttribute("verCode");

        //获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date LoginTime = null;
        try {
            LoginTime = sdf.parse(sdf.format(new Date()));

        } catch (ParseException e) {

            e.printStackTrace();
        }

        BackResult backResult = new BackResult();

        if (vercode.equals(vercodeServer)) {

            UserAdmin result = userAdminService.getUserByUserNameAndPassWord(username, password);
            if (result != null) {

                if (result.getState()==0){
                    backResult.setMsg("账号未通过审核请联系管理员");
                    return backResult;
                }
                //登录成功，将user对象设置到HttpSession作用范围域中
                session.setAttribute("UserAdmin", result);
                Date LastLoginTime =  result.getLoginTime();
                String userName = result.getLoginName();
                Integer UpResult = userAdminService.updateLoginTime(LastLoginTime,LoginTime,userName);

                backResult.setMsg("登录成功");

                return backResult;
            } else {
                backResult.setMsg("用户名或密码不正确");
                return backResult;
            }
        } else {
            backResult.setMsg("验证码不正确");

            return backResult;

        }
    }

    String smsVerify = "0000";
    String smsVerifyState = "false";
    String loginNameState = "false";
    String passWordState = "false";
    String phone = "000";
    String username="";

    @RequestMapping("/checkloginName")
    @ResponseBody
    public BackResult checkloginName(HttpServletRequest request) {
        log.info("核对登录名是否被注册");
        String L_loginname = request.getParameter("L_loginname");
        UserAdmin result = userAdminService.checkloginName(L_loginname);
        BackResult backResult = new BackResult();
        if (result != null) {
            backResult.setMsg("该登录名已被注册");
            return backResult;
        } else {
            backResult.setMsg("该登录名可以使用");
            loginNameState = "ture";
            return backResult;
        }
    }

    @RequestMapping("/checkPhone")
    @ResponseBody
    public BackResult checkPhone(HttpServletRequest request) throws ClientException {
        log.info("核对手机号是否重复");
        phone = request.getParameter("phone");
        UserAdmin result = userAdminService.checkPhone(phone);
        if (result != null) {
            BackResult backResult = new BackResult();
            backResult.setMsg("该手机号已被注册");
            return backResult;
        } else {
           String smsyan= SmsVerify.sms(phone);
//            String smsyan = "6666";
            smsVerify = smsyan;
            BackResult backResult = new BackResult();
            backResult.setMsg("验证码已发送");
            return backResult;
        }

    }


    @RequestMapping("/smsVerifyJudge")
    @ResponseBody
    public Integer smsVerifyJudge(HttpServletRequest request) {
        log.info("手机号验证码验证");
        String PhoneVercode = request.getParameter("L_PhoneVercode");
        if (PhoneVercode.equals(smsVerify)) {
            smsVerifyState = "ture";
            return 1;
        } else {
            return 0;
        }
    }

    @RequestMapping("/adminUserRegister")
    @ResponseBody
    public BackResult adminUserRegister(HttpServletRequest request) {
        log.info("adminUser注册");

        String L_loginname = request.getParameter("L_loginname");
        String L_username = request.getParameter("L_username");
        String L_pass = request.getParameter("L_pass");
        String L_repass = request.getParameter("L_repass");
        String L_email = request.getParameter("L_email");
        String L_phone = phone;

        //获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = null;
        try {
            time = sdf.parse(sdf.format(new Date()));

        } catch (ParseException e) {

            e.printStackTrace();
        }
         //  判断重复输入的密码是否与原密码一致
        if (L_pass.equals(L_repass)) {
            passWordState = "ture";
        }

        UserAdmin userAdmin = new UserAdmin();
        userAdmin.setLoginName(L_loginname);
        userAdmin.setUsername(L_username);
        userAdmin.setPassword(L_pass);
        userAdmin.setEmail(L_email);
        userAdmin.setMobile(L_phone);
        userAdmin.setGenTime(time);

        BackResult backResult = new BackResult();

        if (smsVerifyState == "ture" && loginNameState == "ture" && passWordState == "ture") {

            Integer result = userAdminService.adminUserRegister(userAdmin);
            if (result == 1) {
                backResult.setMsg("注册成功");
                return backResult;
            } else {
                backResult.setMsg("请重新注册");
                return backResult;
            }
        } else {
            backResult.setMsg("手机验证码不正确或用户名已被注册或密码前后不一致");
            return backResult;
        }

    }

    @RequestMapping("/AdminForget")
    public String adminForget(HttpServletRequest request) {
        log.info("管理员忘记用户名密码");
            return "admin/adminForgetSet1";
        }


    @RequestMapping("/AdminForgetPhoneKey")
    @ResponseBody
    public BackResult adminForgetPhoneKey(HttpServletRequest request) {
        log.info("账号找回密码第一步");
      String username = request.getParameter("username");
        String vercode = request.getParameter("vercode");
        HttpSession session = request.getSession();
        String vercodeServer = (String) session.getAttribute("verCode");

        BackResult backResult = new BackResult();
        //        判断验证码是否相同
        if (vercode.equals(vercodeServer)){
            //        查询该账号是否已被注册
            UserAdmin PhoneState = userAdminService.selectByUserName(username);

            if (PhoneState!=null){
                backResult.setMsg("账号验证码正确");
                return  backResult;
            }else {
                backResult.setMsg("该账号未注册");
                return backResult;
            }
        }else {
            backResult.setMsg("验证码不正确");
            return backResult;
        }

    }
//账号忘记密码找回密码发送手机验证码
    @RequestMapping("/adminForgetGetSmsVerify")
    @ResponseBody
    public BackResult adminForgetGetSmsVerify(HttpServletRequest request) throws ClientException {
        log.info("找回密码发送验证码");
        username = request.getParameter("username");
        phone = request.getParameter("phone");
        UserAdmin result = userAdminService.checkPhone(phone);
        BackResult backResult = new BackResult();
        if (result.getLoginName() .equals(username)) {
//            String smsyan= SmsVerify.sms(phone);
            String smsyan = "0000";
            smsVerify = smsyan;
            backResult.setMsg("验证码已发送");
            return backResult;
        } else {
            backResult.setMsg("手机号与账号绑定手机号不匹配");
            return backResult;
        }

    }
//判断找回密码手机验证码是否正确
    @RequestMapping("/ForgetSmsVerifyJudge")
    @ResponseBody
    public Integer ForgetSmsVerifyJudge(HttpServletRequest request) {
        log.info("找回密码验证手机验证码");
        String PhoneVercode = request.getParameter("L_PhoneVercode");
        if (PhoneVercode.equals(smsVerify)) {
            smsVerifyState = "ture";
            return 1;
        } else {
            return 0;
        }
    }
    //判断并进入找回密码3
    @RequestMapping("/ForgetVerifyPhone")
    @ResponseBody
    public Integer ForgetVerifyPhone(HttpServletRequest request) {
        log.info("验证码正确进行跳转");
        if (smsVerifyState == "ture") {
            return 1;
        } else {
            return 0;
        }
    }

    @RequestMapping("/ResetPassword")
    @ResponseBody
    public BackResult ResetPassword(HttpServletRequest request) {
        log.info("重置密码");
        BackResult backResult = new BackResult();
        String firstPsaaword=request.getParameter("firstPassWord");
        String secondPassWord=request.getParameter("secondPassWord");
        if (firstPsaaword.equals(secondPassWord)){
            if (firstPsaaword.length()>5&&firstPsaaword.length()<17){

                if (smsVerifyState=="ture"){
                    Integer result = userAdminService.ResetPassword(phone,firstPsaaword);
                    if (result==1){
                        backResult.setMsg("修改成功");
                        return backResult;
                    }
                }
            }else{
                backResult.setMsg("密码格式不正确");
                return backResult;
            }
        }else{
            backResult.setMsg("两次输入密码不相同");
            return backResult;
        }
        backResult.setMsg("服务器出现错误，请稍后重试");
        return backResult;
    }


}



